<?php


namespace app\common\job;

use app\common\model\live\GoodsModel;
use app\common\model\live\PayModel;
use tencentcloud\Im;
use think\queue\Job;

class Order
{

  /**
   * 检测订单是否付款
   * @param Job $job
   * @param $data
   * @throws \Exception
   */
  public function changePay(Job $job, $data){
    //先删除任务再执行逻辑，防止负载均衡多台服务器重复消费
    $job->delete();
    $params = json_decode($data,true);
    //执行对应逻辑
    $info = PayModel::where('order_sn',$params['order_sn'])->find();
    //未支付订单标识失败，还原库存
    if(!empty($info) && $info['is_pay']!=1){
      PayModel::where('order_sn',$params['order_sn'])->update(['is_fail'=>1]);
      GoodsModel::where('id',$info['goods_id'])->inc('goods_stock',(int)$info['goods_num'])->update();
      GoodsModel::where('id',$info['goods_id'])->update(['is_sale'=>1]);
      //发送刷新商品消息
      $imModel = new Im();
      $imModel->sendGroupMsg($info['room_id'],[
          [
              'MsgType'=>'TIMCustomElem',
              "MsgContent"=>[
                  "Data"=>json_encode(["type"=>"goods_refresh"],JSON_UNESCAPED_UNICODE)
              ]
          ]
      ]);
    }
  }

}